/*
 * parsertemplate.c
 *
 *  Created on: 2013-5-15
 *      Author: xkaying
 */

#include "../typedef/template.h"
#include "parsertemplate.h"

#include "../typedef/token.h"

#include "lexer.h";

#include <assert.h>

#include "parser.h"

Template *ParserTemplate(Lexer *lexer)
{
	assert(lexer);
	int state;
	state = 0;
	ptoken = LexerGetToken(lexer);
	Template *pitem;
	pitem = 0;
	pitem = TemplateInit();
	while (ptoken) {
		switch (state)
		{
			case 100: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						printf("第%d行:错误的模板内容定义.'>'前面多了个','\n", ptoken->line);
						return 0;
					}
					case TokenKey_OPERATOR: {
						//表示是一个操作符号
						TemplateAppend(pitem, 0);
						state = 1;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_CLASS: {
						//表示是一个类
						TemplateAppend(pitem, 1);
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateAppend(pitem, ptoken->name);
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 0: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						ptoken = LexerGetToken(lexer);
						return pitem;
					}
					case TokenKey_OPERATOR: {
						//表示是一个操作符号
						TemplateAppend(pitem, 0);
						state = 1;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_CLASS: {
						//表示是一个类
						TemplateAppend(pitem, 1);
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateAppend(pitem, ptoken->name);
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketStart: {
						//表示( 正式进入读取.
						state = 10;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateItemAppendName(pitem, ptoken->name);
						state = 15;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 10: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 14;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Equl:
					case TokenType_Add:
					case TokenType_Sub:
					case TokenType_Mul:
					case TokenType_Div:
					case TokenType_Mod:
						//二元操作符号 += -= *= /= %=
					case TokenType_EAdd:
					case TokenType_ESub:
					case TokenType_EMul:
					case TokenType_EDiv:
					case TokenType_EMod:
					case TokenType_Inc:
					case TokenType_Dec:
						//移位之类的数据操作 | & ~ ^ << >>
					case TokenType_BitOr:
					case TokenType_BitAnd:
					case TokenType_BitNot:
					case TokenType_BitXor:
					case TokenType_LeftShift:
					case TokenType_RightShift:
					case TokenType_EBitOr:
					case TokenType_EBitAnd:
					case TokenType_EBitXor:
					case TokenType_ELeftShift:
					case TokenType_ERightShift:
						//逻辑操作 || && !
					case TokenType_Or:
					case TokenType_And:
					case TokenType_Not:
						//比较操作符号 > < == >= <= !=
					case TokenType_More:
					case TokenType_Less:
					case TokenType_EE:
					case TokenType_EMore:
					case TokenType_ELess:
					case TokenType_ENot: {
						TemplateItemAppendRange(pitem, (char*) ptoken->type);
						state = 11;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_IndexStart: {
						state = 12;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.模板操作符只能选取可重载操作符.\n",
								ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 11: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						//表示 ,读取下个范围
						state = 12;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						//) 表示范围读取结束
						state = 14;
						ptoken = LexerGetToken(lexer);
						continue;

					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 12: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Equl:
					case TokenType_Add:
					case TokenType_Sub:
					case TokenType_Mul:
					case TokenType_Div:
					case TokenType_Mod:
						//二元操作符号 += -= *= /= %=
					case TokenType_EAdd:
					case TokenType_ESub:
					case TokenType_EMul:
					case TokenType_EDiv:
					case TokenType_EMod:
					case TokenType_Inc:
					case TokenType_Dec:
						//移位之类的数据操作 | & ~ ^ << >>
					case TokenType_BitOr:
					case TokenType_BitAnd:
					case TokenType_BitNot:
					case TokenType_BitXor:
					case TokenType_LeftShift:
					case TokenType_RightShift:
					case TokenType_EBitOr:
					case TokenType_EBitAnd:
					case TokenType_EBitXor:
					case TokenType_ELeftShift:
					case TokenType_ERightShift:
						//逻辑操作 || && !
					case TokenType_Or:
					case TokenType_And:
					case TokenType_Not:
						//比较操作符号 > < == >= <= !=
					case TokenType_More:
					case TokenType_Less:
					case TokenType_EE:
					case TokenType_EMore:
					case TokenType_ELess:
					case TokenType_ENot: {
						TemplateItemAppendRange(pitem, (char*) ptoken->type);
						state = 11;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_IndexStart: {
						state = 13;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.模板操作符只能选取可重载操作符.\n",
								ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 13: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_IndexEnd: {
						TemplateItemAppendRange(pitem,
								(char*) (TokenType_Index));
						state = 11;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.[ 后面缺少 ].\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 14: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateItemAppendName(pitem, ptoken->name);
						state = 15;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 15: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						ptoken = LexerGetToken(lexer);
						return pitem;
					}
					case TokenType_Comma: {
						state = 100;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Equl: {
						//读取模板的默认值
						state = 16;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 16: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Equl:
					case TokenType_Add:
					case TokenType_Sub:
					case TokenType_Mul:
					case TokenType_Div:
					case TokenType_Mod:
						//二元操作符号 += -= *= /= %=
					case TokenType_EAdd:
					case TokenType_ESub:
					case TokenType_EMul:
					case TokenType_EDiv:
					case TokenType_EMod:
					case TokenType_Inc:
					case TokenType_Dec:
						//移位之类的数据操作 | & ~ ^ << >>
					case TokenType_BitOr:
					case TokenType_BitAnd:
					case TokenType_BitNot:
					case TokenType_BitXor:
					case TokenType_LeftShift:
					case TokenType_RightShift:
					case TokenType_EBitOr:
					case TokenType_EBitAnd:
					case TokenType_EBitXor:
					case TokenType_ELeftShift:
					case TokenType_ERightShift:
						//逻辑操作 || && !
					case TokenType_Or:
					case TokenType_And:
					case TokenType_Not:
						//比较操作符号 > < == >= <= !=
					case TokenType_More:
					case TokenType_Less:
					case TokenType_EE:
					case TokenType_EMore:
					case TokenType_ELess:
					case TokenType_ENot: {
						TemplateItemAppendValue(pitem, (char*) ptoken->type);
						state = 17;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_IndexStart: {
						state = 18;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 17: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						ptoken = LexerGetToken(lexer);
						return pitem;
					}
					case TokenType_Comma: {
						state = 100;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 18: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_IndexEnd: {
						TemplateItemAppendValue(pitem,
								(char*) (TokenType_Index));
						state = 17;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.[ 后面缺少 ].\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
				//class 或者 variable
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketStart: {
						//表示( 正式进入读取.
						state = 20;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateItemAppendName(pitem, ptoken->name);
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 20: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 24;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 21;
						TemplateItemAppendRange(pitem, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 21: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comma: {
						//表示 ,读取下个范围
						state = 22;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						//) 表示范围读取结束
						state = 24;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 22: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						printf("第%d行:错误的模板内容定义.')'前面多了一個 ','.\n", ptoken->line);
						return 0;
					}
					case TokenType_Variable: {
						state = 21;
						TemplateItemAppendRange(pitem, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 24: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateItemAppendName(pitem, ptoken->name);
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_More: {
						ptoken = LexerGetToken(lexer);
						return pitem;
					}
					case TokenType_Comma: {
						state = 100;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Equl: {
						//读取模板的默认值
						state = 4;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			case 4: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						TemplateItemAppendValue(pitem, ptoken->name);
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的模板内容定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
			default: {
				printf("parser template状态机错误.状态%d.\n", state);
				return 0;
			}
		}

	}
	printf("第%d行:错误 模板定义没有结尾.\n", ptoken->line);
	return 0;
}

